home *** CD-ROM | disk | FTP | other *** search
- Path: nwgw.infi.net!usenet
- From: Steve Rountree <srndtree@infi.net>
- Newsgroups: comp.lang.c
- Subject: Re: How are multidimensional array stored in memory?
- Date: Fri, 01 Mar 1996 10:04:26 -0800
- Organization: InfiNet
- Message-ID: <31373C2A.4B00@infi.net>
- References: <4h6tk6$1d2@mn5.swip.net>
- NNTP-Posting-Host: h-cairo.dc.infi.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b5 (Win16; I)
-
- My understanding is that multi-dimensional arrays are stored
- contiguously in memory. That is that you can imagine the whole array as
- a single line of data. For example if you had an array of words:
-
- char words[4][31]; /* 4 words each a maximum of 30 chars + NULL */
-
- when you reference the third element, words[2][0], you are actually
- referencing *(words+62).
-
- words[0]: 0 - 30
- words[1]: 31 - 61
- words[2]: 62 - 92
- words[3]: 93 - 123
-
- You can determine its address by multiplying the array desired by the
- length or number of elements within each array. C knows to increment the
- pointer by the sizeof(pointer_type) for each element within the array,
- which in this case is char. The size of a particular data type is
- machine dependent.
-
- Steve Rountree
-